package nckd.wnqjt.tmc.tda.plugin.form;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 获取传入年月第一天
     */
    public static Date getMinMonth(Integer index){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前年
        int year = calendar.get(Calendar.YEAR);
        //设置当前年份
        calendar.set(Calendar.YEAR, year);
        //设置月份
        calendar.set(Calendar.MONTH, index - 1);
        //获取第一天
        int last = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, last);
        Date data = null;
        try {
            data = simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return data;
    }


    /**
     * 获取传入年月最后一天
     */
    public static Date getMaxMonth(Integer index) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前年
        int year = calendar.get(Calendar.YEAR);
        //设置当前年份
        calendar.set(Calendar.YEAR, year);
        //设置月份
        calendar.set(Calendar.MONTH, index - 1);
        //获取第一天
        int last = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, last);
        Date data = null;
        try {
            data = simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return data;
    }
}
